<?php

class InvoiceReportForm extends CFormModel {

    public $startDate;
    public $endDate;
    public $type;
    public $department;

    public function rules() {
        return array(
            array('startDate,endDate', 'required'),
            array('startDate,endDate', 'date', 'format' => 'yyyy-MM-dd'),
            array('type,department','safe')
        );
    }

    public function getDepartmentList() {
        $list = SalesDepartments::model()->findAll('inactive=0');
        $rs = array(
            '' => 'All'
        );
        foreach ($list as $item) {
            $rs[$item->id] = $item->description;
        }
        return $rs;
    }

    public function getData() {
        $crit = new CDbCriteria();
        $crit->compare('invoicetype', $this->type);
        $crit->compare('department', $this->department);
        $crit->addBetweenCondition('invoicedate', $this->startDate, $this->endDate);
        $crit->order = 'CAST( SUBSTR( invoice,1, LOCATE(  "-", invoice )-1) AS SIGNED ) DESC,CAST( SUBSTR( invoice, LOCATE(  "-", invoice ) +1 ) AS SIGNED ) DESC ';
        $invoiceList = Invoice::model()->findAll($crit);
        return $invoiceList;
    }

}

?>
